package com.alliance.shuwei.api;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alliance.shuwei.entity.Category;
import com.alliance.shuwei.service.CategoryService;

@RestController
@RequestMapping(value = "/api/v1/categories", produces = { "application/json" })
public class CategoryResource {
	@Autowired
	private CategoryService categoryService;

	@RequestMapping(method = RequestMethod.GET)
	@ResponseBody
	public List<Category> getRootCategories() {
		return categoryService.getRootCategories();
	}

	@RequestMapping(method = RequestMethod.POST, consumes = { "application/json" })
	@ResponseBody
	public Category createCategories(@RequestBody Category category) {
		categoryService.createCategory(category);
		return category;
	}

	@RequestMapping(value = "/{id}", method = RequestMethod.GET)
	@ResponseBody
	public Category getCategory(@PathVariable("id") Integer id) {
		return categoryService.getCategroy(id);
	}
}
